package com.ax.base.drugstore.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.drugstore.dao.BaseDrugstoreMapper;
import com.ax.base.drugstore.domain.BaseDrugstore;
import com.ax.base.drugstore.service.DrugstoreService;
import com.ax.base.system.domain.MenuDO;
@Service("DrugstoreService")
public class DrugstoreServiceImpl implements DrugstoreService {

	@Autowired
	BaseDrugstoreMapper baseDrugstoreMapper;
	/**
	 * 药监查询(模块:药监人员)*包含该人的药监部门信息
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseDrugstore>
	 */
	@Override
	public List<BaseDrugstore> queryDrugStoreSer(Map<String, Object> params){
		List<BaseDrugstore> baseDrugstores = baseDrugstoreMapper.queryDrugStoreMapper(params);
		return baseDrugstores;
	}
	
	/**
	 * 药房增加(模块:药房管理)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseDrugstore BaseDrugstore
	 */
	@Override
	public BaseDrugstore getBaseDrugstore(Long pid){
		BaseDrugstore baseDrugstores = baseDrugstoreMapper.selectByPrimaryKey(pid);
		String[] dz = baseDrugstores.getAreaCode().split(",");
		baseDrugstores.setProvince(dz[0]);
		baseDrugstores.setCity(dz[1]);
		baseDrugstores.setVillage(dz[2]);
		return baseDrugstores;
	}

	/**
	 * 药房保存(模块:药房管理)
	 * 
	 * @param BaseDrugstore baseDrugstore
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBaseDrugstoreSer(BaseDrugstore baseDrugstore) {
		baseDrugstore.setFirstLetter(PinYinUtil.getFirstSpell(baseDrugstore.getDrugstoreName()));
		baseDrugstoreMapper.insertSelective(baseDrugstore);
	}

	/**
	 * 药房修改(模块:药房管理)
	 * 
	 * @param BaseDrugstore baseDrugstore
	 *           
	 * @return SUCC
	 */	
	@Override
	public void updateBaseDrugstoreSer(BaseDrugstore baseDrugstore) {
		baseDrugstoreMapper.updateByPrimaryKeySelective(baseDrugstore);
	}

	/**
	 * 药店名是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean drugstoreNameValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>();
	    String drugstoreName = pt.substring(0,pt.indexOf(","));
		String parentDrugstoreId = pt.substring(pt.indexOf(",") + 1);
		params.put("drugstoreName",drugstoreName);
		params.put("parentDrugstoreId",parentDrugstoreId);
		boolean validate;
		validate = baseDrugstoreMapper.drugstoreNameValidateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 代码是否重复(新增)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean creditCodeValidateSer(Map<String, Object> params) {
		boolean validate;
		validate = baseDrugstoreMapper.creditCodeValidateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 药店名是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateDrugstoreNameValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		String[] pts = pt.split(",");
		params.put("drugstoreName",pts[0]);
		params.put("parentDrugstoreId",pts[1]);
		params.put("drugstoreId",pts[2]);
		boolean validate;
		validate = baseDrugstoreMapper.updateDrugstoreNameValidateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 代码是否重复(修改)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateCreditCodeValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>();
	    String creditCode = pt.substring(0,pt.indexOf(","));
		String drugstoreId = pt.substring(pt.indexOf(",") + 1);
		params.put("creditCode",creditCode);
		params.put("drugstoreId",drugstoreId);
		boolean validate;
		validate = baseDrugstoreMapper.updateCreditCodeValidateMapper(params).size() > 0;
		return validate;
	}

	/** Tree
	 * 
	 * @param Long parentDrugstoreId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getDrugTree(Long parentDrugstoreId) {
		String drugDepId = parentDrugstoreId.toString();
		List<Tree<MenuDO>> deps = baseDrugstoreMapper.listDrugDepId();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (id.equals(drugDepId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

}
